package com.example.nftapp.act;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.widget.EditText;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.nftapp.R;
import com.example.nftapp.http.HttpCallback;
import com.example.nftapp.http.HttpResponse;
import com.example.nftapp.http.HttpUtil;

import java.util.HashMap;

public class LoginActivity extends AppCompatActivity {

    private EditText edtPhone, edtPwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_login);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        edtPhone = findViewById(R.id.edtPhone);
        edtPwd = findViewById(R.id.edtPwd);
        findViewById(R.id.tvToRegister).setOnClickListener(view -> startActivity(new Intent(this, RegisterActivity.class)));
        findViewById(R.id.btnLogin).setOnClickListener(view -> {
            String username = edtPhone.getText().toString();
            String password = edtPwd.getText().toString();
            HashMap<String, String> map = new HashMap<>();
            map.put("username", username);
            map.put("password", password);
            HttpUtil.doPost("https://www.wanandroid.com/user/login", map, new HttpCallback() {
                @Override
                public void onHttpSucceed(HttpResponse httpResponse) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            startActivity(new Intent(LoginActivity.this, MainActivity.class));
                        }
                    });
                }
            });
        });
    }
}